//
//  CZTabBar.swift
//  WHWeiBo87
//
//  Created by Mac on 16/7/13.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class CZTabBar: UITabBar {
   
    private lazy var composeButton: UIButton = {
        
        let button = UIButton()
        
        //设置图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        //设置背景图片
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
         button.sizeToFit()
         return button
        
    }()
    
    
    override init(frame:CGRect) {
        super.init(frame: frame)
        
        addSubview(composeButton)
    }
   // 加载XIB 或者SB时才调用
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //设置撰写按钮的中心点
        
        composeButton.centerX = width * 0.5
        composeButton.centerY = height * 0.5
        
        //获取系统空间
       // 计算控件的宽度
        let itemWidth = width / 5
        
        //记录遍历是第一个按钮
        var index = 0
        for value in subviews {
            //这个类是私有
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                value.x = CGFloat(index) * itemWidth
                value.width = itemWidth
                if index == 1 {
                    index++
                }
                index++
                
            }
            
        }
        
    }
    
    
    
    
    
    
    
    
    
    
}

























